package com.itcast.controller;

import com.itcast.dao.PublishDao;
import com.itcast.dao.PublishDaoImpl;
import com.itcast.pojo.Friendlist;
import com.itcast.pojo.Publish;
import com.itcast.pojo.User;
import com.itcast.service.FriendService;
import com.itcast.service.FriendServiceImpl;
import com.itcast.service.UserService;
import com.itcast.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@WebServlet("/findFriendServlet")
public class findFriendServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        FriendService friendService = new FriendServiceImpl();
        UserService userService = new UserServiceImpl();

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<User> users = new ArrayList<>();
        List<User> users2 = new ArrayList<>();

        if (session != null) {
            //朋友列表
            List<Friendlist> friendlists = friendService.findAllFriendlist(user.getUserId());
            if (!friendlists.isEmpty()) {
                for (Friendlist friendlist : friendlists) {
                    User user1 = userService.findByUserId(friendlist.getFriendId());
                    users.add(user1);
                }
                request.setAttribute("users", users);
                //推荐朋友列表
                Random random = new Random();
                int n = random.nextInt(friendlists.size());
                Friendlist friendlist = friendlists.get(n);
                List<Friendlist> friendlists_tuijian = friendService.findAllFriendlist(friendlist.getFriendId());
                for (Friendlist friendlist1 : friendlists_tuijian) {
                    User user1 = userService.findByUserId(friendlist1.getFriendId());
                    users2.add(user1);
                }
                request.setAttribute("users2", users2);
            } else if (friendlists.isEmpty()) {
                List<User> users1 = userService.findAllUser();
                session.setAttribute("users2", users1);
            }
            PublishDao publishDao = new PublishDaoImpl();
            List<Publish> publishes = publishDao.findAll();
            request.setAttribute("publishes", publishes);
            request.getRequestDispatcher("/page/space.jsp").forward(request, response);

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if (session != null) {
            doPost(request, response);
        }
    }
}
